﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BrnMall.Core;
using BrnMall.Manager.Domain.Store;
using PetaPoco;

namespace BrnMall.Manager.Manager.Store
{
    /// <summary>
    /// 
    /// </summary>
    public static class StoreWxAppManager
    {
        /// <summary>
        /// 获取小程序默认配置
        /// </summary>
        /// <param name="storeId">店铺ID</param>
        /// <param name="weAppId">小程序配置ID</param>
        /// <returns></returns>
        public static StoreWxApp GetStoreDefaultWxAppById(int storeId, int weAppId)
        {
            if (weAppId > 0)
                return Get(weAppId);
            using (var db = new QDMallDatabase())
            {
                var sqlDefault = new Sql();
                sqlDefault.Select(" top 1 *");
                sqlDefault.From("bma_storewxapp");
                sqlDefault.Where("storeid=@0 and isdefault=1", storeId);
                return db.SingleOrDefault<StoreWxApp>(sqlDefault);
            }
        }
        /// <summary>
        /// 获取单个配置
        /// </summary>
        /// <param name="wid"></param>
        /// <returns></returns>
        public static StoreWxApp Get(int wid)
        {
            using (var db = new QDMallDatabase())
            {
                var storeweapp= db.SingleOrDefault<StoreWxApp>(wid);
                if(storeweapp==null)
                    throw new Exception($"未找到编号{wid}的配置信息");
                return storeweapp;
            }
        }
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="storeId">店铺ID</param>
        /// <returns></returns>
        public static IList<StoreWxApp> GetStoreWxAppList(int storeId)
        {
            using (var db = new QDMallDatabase())
            {
                var sql = new Sql();
                sql.Select("*");
                sql.From("bma_storewxapp");
                sql.Where(" storeid=@0", storeId);
                return db.Fetch<StoreWxApp>(sql);
            }
        }
        /// <summary>
        /// 检查小程序是否已经有存在的设置
        /// </summary>
        /// <param name="weAppId"></param>
        /// <returns></returns>
        public static StoreWxApp GetStoreWxAppByAppId(string weAppId)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<StoreWxApp>("select top 1 * from bma_storewxapp where weappid=@0",weAppId);
            }
        }
        /// <summary>
        /// 更新店铺微信小程序设置
        /// </summary>
        /// <param name="app"></param>
        public static void UpdateOrAddStoreWxApp(StoreWxApp app)
        {

            using (var db = new QDMallDatabase())
            {
                if (app.IsDefault)
                {
                    db.Execute("update bma_storewxapp set isdefault=0 where storeid=@0", app.StoreId);
                }
                if (app.StoreWid < 1)
                {
                    var list = db.Fetch<StoreWxApp>("select * from  bma_storewxapp where storeid=@0", app.StoreId);
                    //if (list.Count >= 2)
                    //    throw new Exception("暂时关闭一个店铺对应多个小程序的功能！！");
                    db.Insert(app);
                }

                else
                    db.Update(app);



            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="wid"></param>

        public static void DeleteStoreWxApp(int wid)
        {
            using (var db = new QDMallDatabase())
            {
                db.Delete<StoreWxApp>(wid);
            }

        }

    }
}
